// 集群，多进程，让多个进程来工作
// 进程也不能无限止的创建，进程它会消耗内存，在有一些应用中，不建议开多进程，
// 进程切换是要消耗cpu  复杂的计算 写入文件
// 进程创建的数量一般和cpu核数相同，最多不要超过4倍
const numCPUs = require('os').cpus().length
const cluster = require('cluster')
// const axios = require('axios')

const http = require('http')
const cheerio = require('cheerio')
const path = require('path')
const fs = require('fs')
const dirpath = path.join(__dirname, 'pics')

const urls = [
  'http://www.jj20.com/bz/nxxz/list_7_1.html',
  'http://www.jj20.com/bz/nxxz/list_7_2.html',
  'http://www.jj20.com/bz/nxxz/list_7_3.html',
  'http://www.jj20.com/bz/nxxz/list_7_4.html',
  'http://www.jj20.com/bz/nxxz/list_7_5.html',
  'http://www.jj20.com/bz/nxxz/list_7_6.html',
  'http://www.jj20.com/bz/nxxz/list_7_7.html',
  'http://www.jj20.com/bz/nxxz/list_7_8.html'
]


if (cluster.isMaster) {
  console.log('主进程 == ' + process.pid);
  for (let i = 0; i < numCPUs; i++) {
    let worker = cluster.fork()
    worker.send(urls[i])
  }

} else {

  // 事件
  process.on('message', url => {
    // 标识
    // 数据抓取
    http.get(url, res => {
      let html = ''
      res.on('data', chunk => html += chunk)
      res.on('end', async () => {
        // 把当前html内容，使用cheerio来完成选择加载s
        const $ = cheerio.load(html)
        const imgs = $('.g-box-1200 > .picbz > li > a > img')
        /* for(let i=0;i<imgs.length;i++){
           let {data} = await axios.get(url)
           fs.writeFileSync(filepath,data)
        }
        process.send('out') */
        imgs.each((index, el) => {
          let src = $(el).attr('src')
          if (src) {
            http.get(src, ret => {
              let name = path.basename(src)
              ret.pipe(fs.createWriteStream(dirpath + '/' + name))
              // process.send('11')
            })
          }
        })
      })
    })
  })
}

let num = numCPUs
cluster.on('message', arg => {
  if (--num === 0) {
    console.log('master === 有返回');
    // 关闭子进程
    cluster.disconnect()
  }
})



